﻿package mortal.game.view.blackMarket
{
    import Message.DB.Tables.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class BlackMarketController extends Controller
    {
        private var _blackMarket:BlackMarketModule;

        public function BlackMarketController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._blackMarket == null)
            {
                this._blackMarket = new BlackMarketModule();
                this._blackMarket.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._blackMarket.addEventListener(WindowEvent.CLOSE, this.onViewClose);
            }
            return this._blackMarket;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.BlckMarketOpen, this.openWin);
            return;
        }// end function

        private function onViewShow(event:Event) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BlackMarketItemUpdate, this.blackMarketItemUpdate);
            Dispatcher.addEventListener(EventName.BlackMarketBuyItem, this.blackMarketBuyItem);
            return;
        }// end function

        private function onViewClose(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BlackMarketItemUpdate, this.blackMarketItemUpdate);
            Dispatcher.removeEventListener(EventName.BlackMarketBuyItem, this.blackMarketBuyItem);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (isViewShow)
            {
                this.view.hide();
            }
            else
            {
                this.view.show();
            }
            return;
        }// end function

        private function blackMarketBuyItem(event:DataEvent) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_2:* = event.data.info as BlackMarketData;
            var _loc_3:* = event.data.item;
            var _loc_4:* = BlackMarketConfig.instance.getBlackMarketConfig(_loc_2.remainInfo.itemCode);
            var _loc_5:* = _loc_2.remainInfo.discount;
            if (Cache.instance.role.enoughMoney(_loc_4.unit, _loc_5))
            {
                _loc_6 = _loc_2.amount;
                _loc_7 = _loc_2.playerInfo ? (_loc_2.playerInfo.amount) : (0);
                _loc_8 = _loc_4.playerAmount - _loc_7;
                if (_loc_6 > _loc_8)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35107), MsgRollTipsType.msgRollTips3);
                }
                GameProxy.guild.buyItemFromBlackMarket(0, _loc_4.itemCode, _loc_6, _loc_3);
            }
            else
            {
                return MsgManager.showRollTipsMsg(Language.getString(35108), MsgRollTipsType.msgRollTips3);
            }
            return;
        }// end function

        public function blackMarketItemUpdate(param1:Object) : void
        {
            if (isViewShow)
            {
                this._blackMarket.updateItem();
            }
            return;
        }// end function

    }
}
